package app.update;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import app.update.utils.PackageUtils;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String channel_id = null;
        String package_name = this.getPackageName();
        int version_code = PackageUtils.getVersionCode(this);
        try {
            channel_id = this.getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA).metaData.getString("UMENG_CHANNEL");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }finally {
            Toast.makeText(this,channel_id + " / " + version_code + "/" + package_name,Toast.LENGTH_LONG).show();
        }

        StringBuilder stringBuilder = new StringBuilder("http://192.168.1.42:3001/check/version/checkVersion");
        stringBuilder.append("?package_name=");
        stringBuilder.append(package_name);
        stringBuilder.append("&channel_id=");
        stringBuilder.append(channel_id);
        stringBuilder.append("&version_code=");
        stringBuilder.append(version_code);

        UpdateAgent agent = new UpdateAgent(this)
                .setAppIcon(R.mipmap.ic_launcher)
                .setCleanMode(true)
                .setSavePath("/sdcard/tmp.apk")
                .setUpdateURL(stringBuilder.toString())
                .setUpdateOnlyWifi(true);
        agent.update();
    }
}
